/**  
* @Title: SecurityService.java
* @Package cn.net.mtons.core.member.security
* @Description: TODO
* @author ma jinjian  
* @date 2016年2月15日 上午10:37:14
* @version V1.0  
*/ 
package cn.net.mtons.core.member.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import cn.net.mtons.model.entity.vo.AccountVO;
import cn.net.mtons.model.entity.vo.ParameterVO;
import cn.net.mtons.persist.interfaces.security.ISecurityDao;

/**
 * @ClassName: SecurityService
 * @Description: TODO
 * @author ma jinjian
 * @date 2016年2月15日 上午10:37:14
 *
 */
public class SecurityService implements UserDetailsService {

	private static Logger logger = Logger.getLogger("command");
	
	@SuppressWarnings("unused")
	private static Logger netLog = Logger.getLogger("netlog");
	
	@Autowired
	private ISecurityDao securityDao;
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		UserDetails user = null;
		ParameterVO vo = new ParameterVO();
		vo.put("username", username);
		AccountVO account = this.securityDao.validate(vo);
		user = new User(account.getUsername(), account.getPassword().toLowerCase(), true, true, true, true, getAuthorities(account.getAccess()));
		return user;
	}

	/**
	* @Title: getAuthorities
	* @Description: 获取权限列表
	* @param @param access
	* @param @return
	* @return Collection<GrantedAuthority> 
	* @throws
	*/ 
	public Collection<GrantedAuthority> getAuthorities(Integer access){
		
		logger.debug("开始获取权限列表");
		
		List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(2);
		
		authorityList.add(new SimpleGrantedAuthority("ROLE_USER"));
		
		if(access.compareTo(1) == 0){
			authorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		}
		
		logger.debug("结束获取权限列表");
		
		return authorityList;
	}

	

}
